package edu.columbia.cloudbox.ui;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import edu.columbia.cloudbox.R;
import edu.columbia.cloudbox.UserStatusService;
import edu.columbia.cloudbox.io.LocalIO;
import edu.columbia.cloudbox.io.S3IO;

public class RegisterActivity extends Activity {
	private final String className = RegisterActivity.class.getSimpleName();

	/** Create view */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);

		final Button register_register = (Button) findViewById(R.id.register_register);
		final Button register_reset = (Button) findViewById(R.id.register_reset);
		final EditText usernameText = (EditText) findViewById(R.id.register_username);
		final EditText passwordText = (EditText) findViewById(R.id.register_password);
		final EditText confirmText = (EditText) findViewById(R.id.register_confirm);

		register_reset.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				usernameText.setText("");
				passwordText.setText("");
				confirmText.setText("");
			}
		});

		register_register.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				String username = usernameText.getText().toString();
				String password = passwordText.getText().toString();
				String confirm = confirmText.getText().toString();

				if (!checkUsername(username)) {
					Notifications.infoDialog(RegisterActivity.this,
							"Register Failed!", "Username cannot be null!");
				} else if (!checkPasswords(password, confirm)) {
					Notifications.infoDialog(RegisterActivity.this,
							"Register Failed!", "Passwords must be the same!");
				} else {
					try {
						String Login_Credential = "username=" + username
								+ "&password=" + password;
						URL url = new URL(
								"http://107.22.164.227:8080/ServletHost/register");
						URLConnection conn = url.openConnection();
						conn.setDoOutput(true);
						OutputStreamWriter wr = new OutputStreamWriter(conn
								.getOutputStream());
						wr.write(Login_Credential);
						wr.flush();

						// Get the response
						InputStream is = conn.getInputStream();

						int c;
						if ((c = is.read()) != -1) {
							Log.i(className,
									"Registered successfully! Redirecting to main activity...");

							UserStatusService.USER_NAME = username;
							UserStatusService.USER_LOGGED_IN = true;
							UserStatusService.S3IO_INSTANCE = new S3IO(is,
									RegisterActivity.this);
							UserStatusService.LocalIO_INSTANCE = new LocalIO(
									username, RegisterActivity.this);

							Intent myIntent = new Intent();
							myIntent.setClass(
									RegisterActivity.this,
									edu.columbia.cloudbox.CloudBoxActivity.class);
							startActivity(myIntent);
						} else {
							Log.i(className, "Register Failed!");
							Notifications.infoDialog(RegisterActivity.this,
									"Register Failed!",
									"Username already existed!");
							usernameText.setText("");
							passwordText.setText("");
							confirmText.setText("");
						}
						Log.i("return", String.valueOf(c));

					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}

	private boolean checkPasswords(String password, String confirm) {
		final String methodName = "checkPasswords(" + password + "," + confirm
				+ ")";
		Log.i(className, methodName + ": Entering...");
		boolean result = !password.equals("") && password.equals(confirm);
		Log.i(className, methodName + ": Result = " + result);
		return result;
	}

	private boolean checkUsername(String username) {
		final String methodName = "checkUsername(" + username + ")";
		Log.i(className, methodName + ": Entering...");

		boolean result = !username.equals("");
		Log.i(className, methodName + ": Result = " + result);

		return result;
	}

}
